package com.gmail.blackdog1987.carcare.util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Bitmap.Config;
import android.graphics.PorterDuff.Mode;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

/**
 * 
 * @author 董国超
 *
 */
public class GraphUtils {

	/**
	 * 读取资源图片
	 * 
	 * @param context 上下文。
	 * @param resId 资源Id。
	 * @param cfg bitmap配置。
	 * @return 图片Bitmap。
	 */
	public static Bitmap getResourceBitmap(Context context, int resId, Config cfg) {
		BitmapFactory.Options opts = new BitmapFactory.Options();
		opts.inPurgeable = true;
		opts.inInputShareable = true;
		opts.inPreferredConfig = cfg;
		InputStream in = context.getResources().openRawResource(resId);
		Bitmap bm = BitmapFactory.decodeStream(in, null, opts);
		try {
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return bm;
	}

	/**
	 * 读取资源图片（RGB_565）
	 * 
	 * @param context 上下文。
	 * @param resId 资源Id。
	 * @return 图片Bitmap。
	 */
	public static Bitmap getResourceBitmap(Context context, int resId) {
		return getResourceBitmap(context, resId, Config.RGB_565);
	}

	/**
	 * 读取Assets图片
	 * 
	 * @param context 上下文。
	 * @param fileName 图片名称。
	 * @param cfg bitmap配置。
	 * @return Bitmap。
	 */
	public static Bitmap getAssetsBitmap(Context context, String fileName, Config cfg) {
		BitmapFactory.Options opts = new BitmapFactory.Options();
		opts.inPurgeable = true;
		opts.inInputShareable = true;
		opts.inPreferredConfig = cfg;
		InputStream in = null;
		try {
			in = context.getAssets().open(fileName);
		} catch (IOException e) {
			e.printStackTrace();
		}
		Bitmap bm = BitmapFactory.decodeStream(in, null, opts);
		try {
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return bm;
	}

	/**
	 * 读取Assets图片（RGB_565）
	 * 
	 * @param context 上下文。
	 * @param fileName 图片名称。
	 * @return Bitmap。
	 */
	public static Bitmap getAssetsBitmap(Context context, String fileName) {
		return getAssetsBitmap(context, fileName, Config.RGB_565);
	}

	/**
	 * Drawable转Bitmap。
	 */
	public static Bitmap drawableToBitmap(Drawable drawable, Config cfg) {
		int w = drawable.getIntrinsicWidth();
		int h = drawable.getIntrinsicHeight();
		Bitmap bitmap = Bitmap.createBitmap(w, h, cfg);
		Canvas canvas = new Canvas(bitmap);
		drawable.setBounds(0, 0, w, h);
		drawable.draw(canvas);
		return bitmap;
	}

	/**
	 * Drawable转Bitmap(RGB_565)。
	 */
	public static Bitmap drawableToBitmap(Drawable drawable) {
		return drawableToBitmap(drawable, Config.RGB_565);
	}

	/**
	 * Bitmap转Byte[]。
	 */
	public static byte[] bitmapToBytes(Bitmap bm) {
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		bm.compress(Bitmap.CompressFormat.PNG, 100, out);
		byte[] data = out.toByteArray();
		try {
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return data;
	}
	
	public static File Bitmap2File(Context context, Bitmap bitmap, String fileName){
		//create a file to write bitmap data  
		File f = new File(context.getCacheDir(), fileName);
		if(bitmap==null){
			return null;
		}
		FileOutputStream fos = null;
		try {
			f.createNewFile(); 
			byte[] bitmapdata = GraphUtils.bitmapToBytes(bitmap);
			fos = new FileOutputStream(f); 
			fos.write(bitmapdata);
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		} finally{
			if(fos!=null){
				try {
					fos.close();
				} catch (IOException e) {
					
				}
			}
		}
		return f;
	}
	
	/**
	 * byte[]转Bitmap。
	 */
	public static Bitmap bytesToBitmap(byte[] b, Config cfg) {
		BitmapFactory.Options opts = new BitmapFactory.Options();
		opts.inPurgeable = true;
		opts.inInputShareable = true;
		opts.inPreferredConfig = cfg;
		return BitmapFactory.decodeByteArray(b, 0, b.length, opts);
	}

	/**
	 * byte[]转Bitmap。
	 */
	public static Bitmap bytesToBitmap(byte[] b) {
		return bytesToBitmap(b, Config.RGB_565);
	}

	/**
	 * 缩放Bitmap。
	 */
	public static Bitmap zoomBitmap(Bitmap bitmap, int width, int height) {
		int w = bitmap.getWidth();
		int h = bitmap.getHeight();
		Matrix matrix = new Matrix();
		float scaleWidth = ((float) width / w);
		float scaleHeight = ((float) height / h);
		matrix.postScale(scaleWidth, scaleHeight);
		Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);
		return newbmp;
	}

	/**
	 * Bitmap转Drawable。
	 */
	@SuppressWarnings("deprecation")
	public static Drawable bitmapToDrawable(Bitmap bm) {
		return new BitmapDrawable(bm);
	}

	/**
	 * 缩放Drawable。
	 */
	@SuppressWarnings("deprecation")
	public static Drawable zoomDrawable(Drawable drawable, int w, int h) {
		int width = drawable.getIntrinsicWidth();
		int height = drawable.getIntrinsicHeight();
		Bitmap oldbmp = drawableToBitmap(drawable);
		Matrix matrix = new Matrix();
		float sx = ((float) w / width);
		float sy = ((float) h / height);
		matrix.postScale(sx, sy);
		Bitmap newbmp = Bitmap.createBitmap(oldbmp, 0, 0, width, height, matrix, true);
		oldbmp = null;
		return new BitmapDrawable(newbmp);
	}

	/**
	 * 获取圆角图片。
	 * 
	 * @param bm 原图片。
	 * @param roundPx 圆角像素。
	 */
	public static Bitmap getRoundedCornerBitmap(Bitmap bm, float roundPx) {
		int w = bm.getWidth();
		int h = bm.getHeight();
		Bitmap output = Bitmap.createBitmap(w, h, bm.getConfig());
		Canvas canvas = new Canvas(output);
		final int color = 0xff424242;
		final Paint paint = new Paint();
		final Rect rect = new Rect(0, 0, w, h);
		final RectF rectF = new RectF(rect);
		paint.setAntiAlias(true);
		canvas.drawARGB(0, 0, 0, 0);
		paint.setColor(color);
		canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
		canvas.drawBitmap(bm, rect, rect, paint);

		return output;
	}

	/**
	 * 获取倒影图片。
	 * 
	 * @param bm 原图片。
	 */
	public static Bitmap createReflectionImageWithOrigin(Bitmap bm) {
		final int reflectionGap = 4;
		int w = bm.getWidth();
		int h = bm.getHeight();
		Matrix matrix = new Matrix();
		matrix.preScale(1, -1);
		Bitmap reflectionImage = Bitmap.createBitmap(bm, 0, h / 2, w, h / 2, matrix, false);
		Bitmap bitmapWithReflection = Bitmap.createBitmap(w, (h + h / 2), bm.getConfig());
		Canvas canvas = new Canvas(bitmapWithReflection);
		canvas.drawBitmap(bm, 0, 0, null);
		Paint deafalutPaint = new Paint();
		canvas.drawRect(0, h, w, h + reflectionGap, deafalutPaint);
		canvas.drawBitmap(reflectionImage, 0, h + reflectionGap, null);
		reflectionImage = null;
		Paint paint = new Paint();
		LinearGradient shader = new LinearGradient(0, bm.getHeight(), 0, bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);
		paint.setShader(shader);
		paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
		canvas.drawRect(0, h, w, bitmapWithReflection.getHeight() + reflectionGap, paint);

		return bitmapWithReflection;
	}

}
